﻿using BlogDemo.Application.PostApp;
using BlogDemo.Application.PostApp.Resource;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlogDemo.Api.Controllers
{
    [Route("api/blog/{blogId}/post")]
    [ApiController]
    public class PostController : ControllerBase
    {

        private readonly IPostAppService postAppService;
        private readonly ILogger<PostController> logger;

        public PostController(IPostAppService postAppService, ILogger<PostController> logger)
        {

            this.postAppService = postAppService;
            this.logger = logger;
        }

        /// <summary>
        /// 获取所有Post
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public  IActionResult  GetPosts(int blogId)
        {
            logger.LogInformation("PostController Log INFO");
            var result = postAppService.GetAll(blogId);
            return Ok(result);
        }

        /// <summary>
        /// 获取单个Post
        /// </summary>
        /// <param name="blogId">博客主键</param>
        /// <param name="postId">文章主键</param>    
        [HttpGet("{postId}")]
        public IActionResult Get(int blogId,int postId)
        {
            if (!postAppService.IsBlogExist(blogId))
            {
                return BadRequest();
            }
            var result = postAppService.Get(postId);
            if (result==null)
            {
                return BadRequest();
            } 
            result.Author += $" BlogID:{blogId}";

            return Ok(result);
        }
        /// <summary>
        /// 新增一个资源
        /// </summary>
        /// <param name="postAdd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Post([FromBody] PostAddResourceModel postAdd)
        {
            this.postAppService.Add(postAdd);
            return Ok();
        }
        /// <summary>
        /// 替换一个Post
        /// </summary>
        /// <param name="id"></param>
        /// <param name="postUpdate">postUpdate</param>
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] PostAddResourceModel postUpdate)
        {

        }

        /// <summary>
        /// 修改一个Post中某些内容
        /// </summary>
        /// <param name="id"></param>
        /// <param name="postPatch"></param>
        [HttpPatch("{id}")]
        public void Patch(int id, [FromBody] PostAddResourceModel postPatch)
        {

        }

        /// <summary>
        /// 删除一个Post
        /// </summary>
        /// <param name="id"></param>
        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            // postAppService.d
        }
    }
}